<?php

namespace app\common\lib;

use think\facade\Db;

class MakePseudo
{
    public $xiangmu = '';
    public $dalei = '';
    public $xiaolei = '';  
    
    public $parent_typeid = 0;
    public $typeid = 0;

    public $keywords = '';  

    public function run($content)
    {
        if(empty($content)){
            return null;
        }
        if(!empty($this->xiangmu)){
            $this->pro = Db::name('data_project')->field('company')->where('title',$this->xiangmu)->find();
            if(!empty($this->pro['company'])){
                $this->com = Db::name('data_company')->where('title',$this->pro['company'])->find();
            }
        }
        $preg = '/\[TAG:(列表.条件|列表.优势|列表.流程|列表.支持|表格.费用|表格.利润|文本.过渡.费用|文本.过渡.利润|图片.大类|图片.小类|图片.项目|图片合成.大类|图片合成.小类|图片合成.项目|文章段落.首段|文章段落.尾段|文章段落.靠谱|文章段落.真假|文章段落.骗局|文章段落.前景|文章段落.生意|文章段落.赔亏|文章段落.怎么样|文章段落.好不好|文章段落.值吗|文章段落.赚钱|文章段落.怎么加盟|文章段落.口碑|公司.名称|公司.成立时间|公司.注册资本|公司.地址|公司.省份|公司.城市|公司.法人)\]/';
        preg_match_all($preg,$content,$match);
        if(in_array('表格.费用',$match[1]) || in_array('表格.利润',$match[1])){
            $make = new \app\common\lib\MakeTable();
            $table = $make->run($this->dalei);
        }
        foreach($match[1] as $k => $v){
            switch ($v) {
                case '列表.条件':
                    $data = $this->getChip(1);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟条件</h2>'.$data;
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '列表.优势':
                    $data = $this->getChip(2);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟优势</h2>'.$data;
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '列表.流程':
                    $data = $this->getChip(3);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟流程</h2>'.$data;
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '列表.支持':
                    $data = $this->getChip(4);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟支持</h2>'.$data;
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '表格.费用':
                    $data = $table['cost'];
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '表格.利润':
                    $data = $table['profit'];
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文本.过渡.费用':
                    $data = $this->getTransition(1);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文本.过渡.利润':
                    $data = $this->getTransition(2);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;

                case '图片.大类':
                    $data = $this->getPic('dalei');
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '图片.小类':
                    $data = $this->getPic('xiaolei');
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '图片.项目':
                    $data = $this->getPic('xiangmu');
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '图片合成.大类':
                    $data = $this->getPicMerge('dalei');
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '图片合成.小类':
                    $data = $this->getPicMerge('xiaolei');
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '图片合成.项目':
                    $data = $this->getPicMerge('xiangmu');
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;

                case '文章段落.首段':
                    $data = $this->getTextStart();
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.尾段':
                    $data = $this->getTextEnd();
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.靠谱':
                    $data = $this->getSectionTitle(1).'<p>'.$this->getTextSection(1).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.真假':
                    $data = $this->getSectionTitle(2).'<p>'.$this->getTextSection(2).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.骗局':
                    $data = $this->getSectionTitle(3).'<p>'.$this->getTextSection(3).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.前景':
                    $data = $this->getSectionTitle(4).'<p>'.$this->getTextSection(4).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.生意':
                    $data = $this->getSectionTitle(5).'<p>'.$this->getTextSection(5).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.赔亏':
                    $data = $this->getSectionTitle(6).'<p>'.$this->getTextSection(6).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.怎么样':
                    $data = $this->getSectionTitle(7).'<p>'.$this->getTextSection(7).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.好不好':
                    $data = $this->getSectionTitle(8).'<p>'.$this->getTextSection(8).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.值吗':
                    $data = $this->getSectionTitle(9).'<p>'.$this->getTextSection(9).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.赚钱':
                    $data = $this->getSectionTitle(10).'<p>'.$this->getTextSection(10).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.怎么加盟':
                    $data = $this->getSectionTitle(11).'<p>'.$this->getTextSection(11).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '文章段落.口碑':
                    $data = $this->getSectionTitle(12).'<p>'.$this->getTextSection(12).'</p>';
                    $data = $this->replace_content($data);
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;

                case '公司.名称':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['title'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司.成立时间':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['regDate'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司.注册资本':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['regMoney'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司.地址':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['address'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司.省份':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['province'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司.城市':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['city'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司.法人':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['faRen'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
            }
        }
        return $content;
    }

    private function getTransition($position){
        if(!empty($this->typeid)){
            $res = Db::name('data_text_transition')->field('content')->where([
                'typeid' => $this->typeid,
                'position' => $position
            ])->orderRaw('rand()')->find();
        }
        if(empty($res)){
            if(!empty($this->parent_typeid)){
                $res = Db::name('data_text_transition')->field('content')->where([
                    'parent_typeid' => $this->parent_typeid,
                    'position' => $position
                ])->orderRaw('rand()')->find();
            }
        }
        if(empty($res)){
            return '';
        }
        return str_replace('{xiangmu}', $this->xiangmu, $res['content']);
    }

    private function getTextStart(){
        // if(!empty($this->typeid)){
        //     $res = Db::name('data_text_start')->field('content')->where([
        //         'typeid' => $this->typeid
        //     ])->orderRaw('rand()')->find();
        // }
        // if(empty($res)){
        //     if(!empty($this->parent_typeid)){
        //         $res = Db::name('data_text_start')->field('content')->where([
        //             'parent_typeid' => $this->parent_typeid
        //         ])->orderRaw('rand()')->find();
        //     }
        // }
        $res = Db::name('data_text_start')->field('content')->orderRaw('rand()')->find();
        if(empty($res)){
            return '';
        }
        $res['content'] = $this->replace_content($res['content']);
        return $res['content'];
    }

    private function getTextEnd(){
        // if(!empty($this->typeid)){
        //     $res = Db::name('data_text_end')->field('content')->where([
        //         'typeid' => $this->typeid
        //     ])->orderRaw('rand()')->find();
        // }
        // if(empty($res)){
        //     if(!empty($this->parent_typeid)){
        //         $res = Db::name('data_text_end')->field('content')->where([
        //             'parent_typeid' => $this->parent_typeid
        //         ])->orderRaw('rand()')->find();
        //     }
        // }
        $res = Db::name('data_text_end')->field('content')->orderRaw('rand()')->find();
        if(empty($res)){
            return '';
        }
        $res['content'] = $this->replace_content($res['content']);
        return $res['content'];
    }

    private function getTextSection($position){
        $res = Db::name('data_text_section')->field('content')->where('position',$position)->orderRaw('rand()')->find();
        if(empty($res)){
            return '';
        }
        return $res['content'];
    }

    private function getSectionTitle($position){
        $data = '';
        $title = Db::name('data_text_section_title')->field('content')->where('position',$position)->orderRaw('rand()')->find();
        if(!empty($title)){
            $data = '<h2>'.$title['content'].'</h2>';
        }
        return $data;
    }

    private function replace_content($content){
        $preg = '/\[替换:(所属大类|所属小类|项目名称|公司名称|公司成立时间|公司地址|关键词)\]/';
        preg_match_all($preg,$content,$match);
        foreach($match[1] as $k => $v){
            switch ($v) {
                case '所属大类':
                    $content = str_replace_once($match[0][$k],$this->dalei,$content);
                break;
                case '所属小类':
                    $content = str_replace_once($match[0][$k],$this->xiaolei,$content);
                break;
                case '项目名称':
                    $content = str_replace_once($match[0][$k],$this->xiangmu,$content);
                break;
                case '公司名称':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['title'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司成立时间':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['regDate'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '公司地址':
                    $data = '';
                    if(!empty($this->com)){
                        $data = $this->com['address'];
                    }
                    $content = str_replace_once($match[0][$k],$data,$content);
                break;
                case '关键词':
                    $content = str_replace_once($match[0][$k],$this->keywords,$content);
                break;
            }
        }
        return $content;
    }

    private function getPic($type){
        switch ($type) {
            case 'dalei':
                if(!empty($this->parent_typeid)){
                    $res = Db::name('data_pic_type')->field('url')->where([
                        'parent_typeid' => $this->parent_typeid
                    ])->orderRaw('rand()')->find();
                }
            break;
            case 'xiaolei':
                if(!empty($this->typeid)){
                    $res = Db::name('data_pic_type')->field('url')->where([
                        'typeid' => $this->typeid
                    ])->orderRaw('rand()')->find();
                }
            break;
            case 'xiangmu':
                if(!empty($this->xiangmu)){
                    $res = Db::name('data_pic_project')->field('url')->where([
                        'project' => $this->xiangmu
                    ])->orderRaw('rand()')->find();
                }
            break;
        }
        if(empty($res)){
            return '';
        }
        return '<p style="text-align: center;"><img src="'.$res['url'].'" /></p>';
    }

    private function getPicMerge($type){
        switch ($type) {
            case 'dalei':
                if(!empty($this->parent_typeid)){
                    $res = Db::name('data_pic_merge')->field('url')->where([
                        'parent_typeid' => $this->parent_typeid,
                        'typeid' => 0,
                        'project' => ''
                    ])->orderRaw('rand()')->find();
                }
            break;
            case 'xiaolei':
                if(!empty($this->typeid)){
                    $res = Db::name('data_pic_merge')->field('url')->where([
                        'typeid' => $this->typeid,
                        'project' => ''
                    ])->orderRaw('rand()')->find();
                }
            break;
            case 'xiangmu':
                if(!empty($this->xiangmu)){
                    $res = Db::name('data_pic_merge')->field('url')->where([
                        'project' => $this->xiangmu
                    ])->orderRaw('rand()')->find();
                }
            break;
        }
        if(empty($res)){
            return '';
        }
        $merge = new \app\common\lib\MergePic();
        $filepath = $merge->open($res['url'])->vague()->mask()->text($this->xiangmu, '17shangji.com')->save();
        return '<p style="text-align: center;"><img src="'.$filepath.'" /></p>';
    }

    //获取 条件 优势 流程
    private function getChip($position){
        $str = '';
        if($position == 3){
            $tag = 'ol';
        }else{
            $tag = 'ul';
        }
        $make = new \app\common\lib\MakeChip();
        $data = $make->run($position);
        if(!empty($data)){
            $num = 0;
            $count = count($data);
            $str .= '<'.$tag.'>';
            foreach ($data as $v) {
                $num++;
                if($num==$count){
                    $v .= '。';
                }else{
                    $v .= '；';
                }
                $v = str_replace('www', '17商机网', $v);
                $v = str_replace('xxx', $this->xiangmu, $v);
                $v = str_replace('dalei', $this->dalei, $v);
                $v = str_replace('xiaolei', $this->xiaolei, $v);
                $str .= '<li>'.$v.'</li>';
            }
            $str .= '</'.$tag.'>';
        }
        return $str;
    }


}